; ; (require :agraph)

(in-package :db.agraph.user)  

(enable-!-reader)  
(register-namespace "ex" "http://www.franz.com/simple#")  
(register-namespace "pm" "http://www.possibly-sam.com/pm/2010/8/pm-03#")
(register-namespace "pmi" "http://www.possibly-sam.com/pm/2010/8/pmi-03#")
(register-namespace "t" "http://franz.com/ns/allegrograph/3.0/temporal/") 
(register-namespace "cyc" "http://www.possibly-sam.com/cyc/2010/8/cyc-01#")

(setf *get-triples-list-limit* 999)
(setf *print-tripbles-list-limit* 999)

; (print-triples  (get-triples-list) :format :terse)  
(db.agraph.user::display-namespaces) 

db.agraph.user::*default-pathname-defaults*
(format t "~a" db.agraph.user::*default-pathname-defaults*)
(setf db.agraph.user::*default-pathname-defaults* "D:\\r\\")
(gen-res::cnu)
(gen-res::cnu 4)
(gen-res::cnu 3 nil)    ; use this to keep open.



(defun all-triples (&optional (format :terse)) (print-triples (get-triples-list) :format format))

(defun att () (all-triples :terse))
(defun atc () (all-triples :concise))
(defun atl () (all-triples :long))

;(defun print-lines ( l &optional out) (format out "~{~a~%~}" l))
;(defun print-tabs ( l &optional out) (format out "~{~a~5,5T~}" l))

(defun print-with-tab( e &optional out )
  (format out "~a~a" e #\tab))

(defun print-with-cr ( l &optional out)
  (mapcar #'(lambda(x) (print-with-tab x out)) l)
  (format out "~%"))

(defun pwcr (l &optional out)
  (mapcar #'(lambda(x) (print-with-cr x out)) l)
  (length l))

(defun print-nested-file (l file-name)
  (with-open-file (out file-name :direction :output :if-exists :supersede)
    (pwcr l out))
  (r-run::xl file-name)
  )
  


; (defun print-nested( l &optional out) (format out "~{~{~a#\tab~}~%~}" l))
; one bird in (a nest) (@#$egg"\\|} tab://&optional omellette ~%~scrambled"}} format or table


(defun acha (n) 
  (with-open-file (out "temp.txt" :direction :output :if-exists :supersede)
  (print-nested n out)))

(defun list-all-times () 
  (select (?n ?t ?y)  
          (q- ?z !pm:NAME ?n)
          (q- ?z !pm:DATE ?d)
          (q- ?d !t:time ?t)
          (upi-to-date-string ?t ?y)
          ) ; end select
        )

(defun list-all-milestones  () 
  (select (?n ?y ?o)  
          (q- ?z !rdf:type !pm:MILE-STONE)
          (q- ?z !pm:NAME ?n)
          (q- ?z !pm:OWNER ?i)
          (q- ?i !pm:NAME ?o)
          (q- ?z !pm:DATE ?d)
          (q- ?d !t:time ?t)
          (upi-to-date-string ?t ?y)
          ) ; end select
        )

(select (?i ?d) (q- ?i !rdf:type !pm:AGENDA-ITEM) (q- ?i !pm:DONE ?d))

; done-ness
(<-- (is-done ?x) (q- ?x !pm:DONE !"1"^^xs:integer ))
(<-- (is-not-done ?x) (not is-done ?x))

; name
(<-- (name-of ?name ?i) (q- ?i !pm:NAME ?name))
(<- (name-of "NA" ?i))

; date
(<-- (date-of ?date ?i) (q- ?i !pm:DATE ?d) (q- ?d !t:time ?t) (upi-to-date-string ?t ?date))
(<- (date-of "NA" ?i))

(<-- (is-milestone ?i) (q- ?i !rdf:type !pm:MILE-STONE))

(<-- (owner-of ?owner ?i) (q- ?i !pm:OWNER ?o) (name-of ?owner ?o))
(<- (owner-of "NA" ?i))

(defun unfinished-items ()
  (select (?name ?date ?owner)
          (is-milestone ?i)
          (is-not-done ?i)
          (name-of ?name ?i)
          (date-of ?date ?i)
          (owner-of ?owner ?i)
                 
  ))

(defun ms-applying-to ()
  (select (?n ?m ?s ?d) 
           (q- ?y !rdf:type !pm:AGENDA-ITEM)
           (q- ?y !pm:NAME ?n)
           (q- ?y !pm:TO-BE-DISCUSSED-IN ?j)
          (q- ?j !pm:NAME  ?m)
           (q- ?y !pm:APPLIES-TO-SUBSYSTEM ?i)
          (q- ?i !pm:NAME  ?s)
          (q- ?y !pm:DONE ?d)
           ))


(defmacro ms-applying-to (n) 
  `(select (?x) 
           (q- ?y !rdf:type !pm:MILE-STONE)
           (q- ?y !pm:NAME ?x)
           (q- ?y !pm:APPLIES-TO-SUBSYSTEM ,n)
           ))

(select (?x) 
        (q- ?y !pm:NAME ?x)
        )

(select (?y) 
        (q- ?y !pm:NAME !"D005-LOAD-2")
        )



(defun list-all-milestones-dates () 
  (select (?x)  
          (q- ?y !rdf:type !pm:Milestone)
          (q- ?y !t:time ?x)
         )
  
  )


(defun list-all-milestones-before () 
  (select (?x)  
          (q- ?y !rdf:type !pm:Milestone)
          (q- ?y !pm:name ?x)
          (point-before ?y !ex:xmas)
         )
  
  )

(defun read-and-store-milestone (in)
  (let* 
      (
       ( m (make-instance 'mile-stone :id (gensym)))
       
       )
    (read-me m in)
    (store-mile-stone m)
    )
  )

(defun read-whole-milestone-file ()
  (let*
      (
       (in (open "C:\\allegro-projects\\rdf-01\\some-milestones.txt"))
       (the-size (read in))
       )
    (loop for k from 1 to the-size do (read-and-store-milestone in))
    (index-all-triples)
    
    )
  )


(defun delete-my-triples (b e)
  (loop for k from b to e do (delete-triple k)))

(defun reset-milestone-file () (open "C:\\allegro-projects\\rdf-01\\some-milestones.txt"))

 ( db.agraph.user::create-triple-store "time-trials-01" )

(add-triple !ex:pt1 !t:time (date-string-to-upi "2008-02-01"))  
(add-triple !ex:pt2 !t:time (date-string-to-upi "2008-02-02"))  
(add-triple !ex:pt3 !t:time (date-string-to-upi "2008-02-03"))  
(add-triple !ex:pt4 !t:time (date-string-to-upi "2008-02-04"))  
(add-triple !ex:pt5 !t:time (date-string-to-upi "2008-02-05"))  
(add-triple !ex:pt6 !t:time (date-string-to-upi "2008-02-06"))  
(add-triple !ex:pt7 !t:time (date-string-to-upi "2008-02-07"))  
(add-triple !ex:holloween !t:time (date-string-to-upi "2010-10-31"))  
(add-triple !ex:xmas !t:time (date-string-to-upi "2010-12-25")) 


(defmacro get-points-before (n)
  `( select0 (?x ?y) 
            (q- ?pt !pm:name ?x)
            (q- ?pt !pm:owner ?y)
           (point-before ?pt ,n)
           ))

(select0 (?pt)    (point-before ?pt !ex:holloween))  

(defun list-all-points () 
  (select (?pt) (point-simultaneous ?pt ?pt)))

(defun destroy-everything () 
  (labels
      (
       (fn (n) (delete-triple (triple-id n)))
       )
           (mapcar  #'fn (get-triples-list))
    
    )
  )

(defmacro sexy (n  ms) (n ms))


 (defvar one-conv  "Abbott, Gayatri [6:24 PM]:
  hmm...lots to do but give me 5
  10 actually
Abbott, Phillip [6:25 PM]:
  ok
Abbott, Gayatri [7:05 PM]:
  5 mins more just saving and shutting down
Abbott, Phillip [7:05 PM]:
  ok c u @ car
Abbott, Gayatri [7:11 PM]:
  ok
Abbott, Phillip [7:16 PM]:
  ok
Abbott, Gayatri [7:17 PM]:
  ok
Abbott, Phillip [7:17 PM]:")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun fill-kennedy-db ()   
    (create-triple-store "kennedy-09"
                         :if-exists :supersede)  
    (time  
     (load-ntriples #p"C:\\acl82express\\agraph\\python\\franz\\openrdf\\tests\\kennedy.ntriples"))  
    (index-all-triples))  

   

(<-- (male ?x)  
    (q- ?x !ex:sex !ex:male))  

(<-- (female ?x)  
    (q- ?x !ex:sex !ex:female))  

(<-- (father ?x ?y)  
    (male ?x)  
    (q- ?x !ex:has-child ?y))  

(<-- (mother ?x ?y)  
    (female ?x)  
    (q- ?x !ex:has-child ?y))  

(<-- (parent ?x ?y) 
     (father ?x ?y))  
(<- (parent ?x ?y) 
    (mother ?x ?y))  


(<-- (grandparent ?x ?y)  
    (parent ?x ?z)  
    (parent ?z ?y))  

(<-- (grandchild ?x ?y)  
    (grandparent ?y ?x)) 


(<-- (ancestor ?x ?y)  
    (parent ?x ?y))  

(<-  (ancestor ?x ?y)      
    (parent ?x ?z)  
    (ancestor ?z ?y)) 



(setq cel (list 'res 'gen-res 'name "Celtrak"))

(setq first-sr3-serial-number 5001174888)